<template>
  <div>
    <el-form ref="ruleForm" :model="carousel" label-width="100px">
      <el-form-item label="轮播图名称" prop="carouselName">
        <el-input v-model="carousel.carouselName" />
      </el-form-item>
      <el-form-item v-if="uploadVisible" label="轮播图上传" prop="blogImage">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
        >
          <img v-if="carousel.carouselImgUrl" :src="carousel.carouselImgUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="跳转地址" prop="carouselUrl">
        <el-select v-model="carousel.carouselUrl" placeholder="请选择要跳转到的博客地址" style="width: 100%" @focus="getAllBlogNameForRouter">
          <el-option
            v-for="item in blogNameList"
            :key="item.blogUrl"
            :label="item.blogName"
            :value="item.blogUrl"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提交" label-width="100px">
        <el-button type="primary" @click="updateCarousel()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import carouselApi from '../../../api/carousel'
import { getToken } from '@/utils/auth'
export default {
  name: 'CarouselUpdate',
  props: {
    carousel: {
      carousel: Object,
      default: null
    }
  },
  data() {
    return {
      uploadVisible: true,
      headers: {
        Authorization: getToken()
      },
      uploadUrl: process.env.VUE_APP_UPLOAD_URL,
      blogNameList: []
    }
  },
  created() {
    this.getAllBlogNameForRouter()
  },
  methods: {
    getAllBlogNameForRouter() {
      carouselApi.getAllBlogNameForRouter().then(res => {
        if (res.code === 200) {
          this.blogNameList = res.data
        }
      })
    },
    lunbotuFocus() {
    },
    updateCarousel() {
      carouselApi.update(this.carousel).then(res => {
        if (res.code === 200) {
          this.$emit('getCarouselList')
          this.$emit('closeUpdateDialog')
          this.$message.success(res.msg)
          carouselApi.getList().then(res => {
            this.$store.commit('carouselList/SET_TYPELIST', res.data)
          })
        }
      })
    },
    lunbotunBlur() {
      if (this.carousel.carouselImgUrl.trim().length > 0) {
        this.uploadVisible = false
      }
    },
    handleUploadSuccess(res, file) {
      this.imageUrl = res.data
      this.carousel.carouselImgUrl = res.data
    }
  }
}
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
